package com.sixth.util;

import com.sixth.bean.MarketSystem;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/8 14:50
 **/


public class ConfigUtils {

    public static List<MarketSystem> getChanges(Map<String, Object> map, List<MarketSystem> marketSystems) {
        ArrayList<MarketSystem> list = new ArrayList<>();
        for (MarketSystem marketSystem : marketSystems) {
            if (!map.get(marketSystem.getKeyName()).equals(marketSystem.getKeyValue())) {
                marketSystem.setKeyValue((String) map.get(marketSystem.getKeyName()));
                marketSystem.setUpdateTime(new Date());
                list.add(marketSystem);
            }
        }
        return list;
    }

    public static void getConfig(List<MarketSystem> marketSystems,Object target){
        Class<?> targetClass = target.getClass();

        for (MarketSystem marketSystem : marketSystems) {
            String keyName = marketSystem.getKeyName();
            try {
                Field declaredField = targetClass.getDeclaredField(keyName);
                declaredField.setAccessible(true);
                declaredField.set(target,marketSystem.getKeyValue());
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }

    }

    public static void getConfig(Map<String,Object> map,Object target){
        Class<?> targetClass = target.getClass();
        for (String s : map.keySet()) {
            try {
                Field declaredField = targetClass.getDeclaredField(s);
                declaredField.setAccessible(true);
                declaredField.set(target,map.get(s));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }

        }

    }
}
